Skip to content

gh-104533: Add a dataclass-like decorator for ctypes structures#153781

Open
ZeroIntensity wants to merge 10 commits into
python:mainfrom
ZeroIntensity:ctypes/struct-decorator
Open

gh-104533: Add a dataclass-like decorator for ctypes structures#153781
ZeroIntensity wants to merge 10 commits into
python:mainfrom
ZeroIntensity:ctypes/struct-decorator

Conversation

@ZeroIntensity

@ZeroIntensity ZeroIntensity commented Jul 15, 2026

Copy link
Copy Markdown
Member

@read-the-docs-community

read-the-docs-community Bot commented Jul 15, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33620062 | 📁 Comparing 2e469b6 against main (b4662e8)

  🔍 Preview build  

3 files changed
± library/ctypes.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
@ZeroIntensity ZeroIntensity marked this pull request as ready for review July 16, 2026 09:39
@ZeroIntensity ZeroIntensity requested a review from AA-Turner as a code owner July 16, 2026 09:39
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py
Comment on lines +561 to +576
def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None):
process_the_struct = functools.partial(
_process_struct,
align=align,
layout=layout,
endian=endian,
pack=pack
)

if class_or_none is None:
def inner(klass):
return process_the_struct(klass)

return inner

return process_the_struct(class_or_none)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it like this:

Suggested change
def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None):
process_the_struct = functools.partial(
_process_struct,
align=align,
layout=layout,
endian=endian,
pack=pack
)
if class_or_none is None:
def inner(klass):
return process_the_struct(klass)
return inner
return process_the_struct(class_or_none)
def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None):
if class_or_none is None:
return functools.partial(
struct,
align=align,
layout=layout,
endian=endian,
pack=pack,
)
return process_the_struct(class_or_none)

That's how decorators of this fashion are usually made.
Feel free to ignore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process_the_struct isn't defined here; what are you trying to show?

Comment thread Doc/library/ctypes.rst
Comment on lines +3164 to +3165
.. decorator:: struct(*, align=None, layout, endian='native', pack=None)
:module: ctypes.util

@johnslavik johnslavik Jul 16, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a collation: old syntax vs. new syntax.

@ZeroIntensity ZeroIntensity Jul 16, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's necessary -- the examples for Structure are right above.

Comment thread Lib/ctypes/util.py Outdated
ZeroIntensity and others added 2 commits July 16, 2026 12:01
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants